<script lang="ts" setup>
import userStore from '@/store';
import { ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router';
import GoodsImage from './components/goods-image.vue';
import GoodsSales from './components/goods-sales.vue';
import GoodsName from './components/goods-name.vue';
import GoodsSku from './components/goods-sku.vue';
import GoodsDetail from './components/goods-detail.vue';
import GoodsHot from './components/goods-hot.vue';
import { XtxMessage } from '@/components/message/message';
const { goods,cart} = userStore()
const route = useRoute()
const currentId = ref('')
watchEffect(()=>{
  if(route.fullPath !== '/goods/' + route.params.id) return
  goods.resetGoodsInfo()
  goods.getGoodsInfo(route.params.id as string)
})
const hchange = (skuId:string)=>{
  currentId.value = skuId
  const sku = goods.info.skus.find(item=>item.id === skuId)
  if(!sku) return
  goods.info.price =sku.price
  goods.info.oldPrice = sku.oldPrice

}
const count = ref(1)

const addCarts = ()=>{
    if(!currentId.value) return XtxMessage.warning('请选择全部规格')
   cart.addCart({
    skuId: currentId.value,
    count: count.value
   })
   XtxMessage.success('添加成功')
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 面包屑 -->
      <div style="height: 70px;" >
      <Transition name="fade-in-out">
      <XtxBread v-if="goods.info.categories">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.info.categories[1].id}`">{{ goods.info.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.info.categories[0].id}`">{{ goods.info.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem >{{ goods.info.name }}</XtxBreadItem>
      </XtxBread>
      <XtxBread v-else >
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>
              <XtxSkeleton opacity="0.2" bg="#27bb9a" animated :width="28" :height="18" />
            </XtxBreadItem>
            <XtxBreadItem>
              <XtxSkeleton opacity="0.2" bg="#27bb9a" animated :width="76" :height="18" />
            </XtxBreadItem>
            <XtxBreadItem>
              <XtxSkeleton opacity="0.2" bg="#27bb9a" animated :width="156" :height="18" />
            </XtxBreadItem>
      </XtxBread>
      </Transition>
      </div>
      <!-- 商品信息 -->
     <!-- 商品信息 -->
        <div v-if="goods.info.mainPictures"  class="goods-info">
          <div class="media">
            <GoodsImage :images="goods.info.mainPictures"></GoodsImage>
            <GoodsSales />
          </div>
          <div class="spec">
            <GoodsName  :goods='goods.info'/>
              <!-- 
            1. 传入数据会渲染 (无库存禁用)
            2. 传入 skuId 会自动勾选, 无货不会勾选
            3. 当全部规格勾选后会返回一个 skuId 供父组件使用
           -->
            <GoodsSku @change-sku-id="hchange" :skuId="'1369155864430120962'" :goods='goods.info'/>
            <XtxNumbox v-model='count' :max="goods.info.inventory" :min="1" show-lable/>
            <XtxButton type="primary" style="margin-top: 20px;" @click="addCarts">加入购物车</XtxButton>
          </div>
        </div>
      <!-- 商品详情 -->
      <div class="goods-footer" v-if="goods.info.details">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goods.info" ></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>